package com.example.libraryms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.libraryms.model.dto.BookTypeAddDTO;
import com.example.libraryms.model.dto.BookTypeDTO;
import com.example.libraryms.model.dto.BookTypeQueryDTO;
import com.example.libraryms.model.dto.BookTypeUpdateDTO;
import com.example.libraryms.model.entity.BookType;
import com.example.libraryms.service.BookTypeService;
import com.example.libraryms.utils.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cauli
 * @date 2023/7/31 11:53
 * @description 图书分类 前端控制器
 */
@Api(tags = "图书分类")
@ApiSort(2)
@RestController
@RequestMapping("/bookType")
public class BookTypeController {
    @Autowired
    private BookTypeService bookTypeService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取图书分类列表")
    @GetMapping("/list")
    public ResultResponse<BookType> listBookType(BookTypeQueryDTO bookTypeQueryDTO) {
        Page<BookType> bookTypePage = bookTypeService.getBookTypePage(bookTypeQueryDTO);
        return ResultResponse.success().dataList(bookTypePage.getRecords(), bookTypePage.getTotal());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "根据图书分类编号获取详细信息")
    @GetMapping(value = {"/{bookTypeNo}"})
    public ResultResponse<BookTypeDTO> getBookTypeDetail(@PathVariable(value = "bookTypeNo", required = false) Integer bookTypeNo) {
        BookTypeDTO bookTypeDTO = bookTypeService.getBookType(bookTypeNo);
        return ResultResponse.success().data(bookTypeDTO);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "添加图书分类")
    @PostMapping
    public ResultResponse<Void> addBookType(@RequestBody BookTypeAddDTO bookTypeAddDTO) {
        bookTypeService.addBookType(bookTypeAddDTO);
        return ResultResponse.success().message("添加成功");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改图书分类")
    @PutMapping
    public ResultResponse<Void> updateBookType(@RequestBody BookTypeUpdateDTO bookTypeUpdateDTO) {
        bookTypeService.updateBookType(bookTypeUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除图书分类")
    @DeleteMapping("/{bookTypeNoList}")
    public ResultResponse<Void> deleteBook(@PathVariable List<Integer> bookTypeNoList) {
        bookTypeService.deleteBookTypes(bookTypeNoList);
        return ResultResponse.success().message("删除成功");
    }
}
